/*
 * @Author: kanglang
 * @Date: 2021-03-05 10:41:17
 * @LastEditors: kanglang
 * @LastEditTime: 2021-03-15 10:08:03
 * @Description: 油卡核销结果
 */

import React, { Component } from 'react';
import { View, SafeAreaView, Text, StyleSheet, Image, TouchableOpacity } from 'react-native';
import {
  iconResultFailure,
  iconResultSuccess
} from '@/images';
import { px2dp, mainBgColorWhite, fontColorCoffee, fontColorBlack } from '@/styles';
import { msg, cache } from '@/common';
import api from '@/api';

export default class WriteOffResult extends Component {
  constructor(props) {
    super(props);
    const { navigation } = props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { result } = params;
    this.state = {
      result, // 0 失败 1 成功
      oilcardAuthority: false,
      isAlliance: false,
      isLogin: false
    };
  }

  componentDidMount() { }

  render() {
    const { result } = this.state;
    return (
      <SafeAreaView style={styles.container}>
        <View style={{ alignItems: 'center' }}>
          <Image
            style={{
              width: px2dp(294),
              height: px2dp(294)
            }}
            source={result ? iconResultSuccess : iconResultFailure}
          />
          <Text
            style={{
              fontSize: px2dp(40),
              fontWeight: 'bold'
            }}
          >
            {result ? '核销成功' : '核销失败'}
          </Text>
        </View>
        <View>
          <TouchableOpacity
            style={styles.btn}
            onPress={this.reScan}
          >
            <Text style={{ color: mainBgColorWhite, fontSize: px2dp(32) }}>返回扫一扫</Text>
          </TouchableOpacity>
          <TouchableOpacity
            style={[
              styles.btn,
              {
                backgroundColor: mainBgColorWhite,
                marginTop: px2dp(50),
                borderWidth: px2dp(1),
                borderColor: '#CCCAC8'
              }
            ]}
            onPress={this.goHome}
          >
            <Text style={{ color: fontColorBlack, fontSize: px2dp(32) }}>返回首页</Text>
          </TouchableOpacity>
        </View>
      </SafeAreaView>
    );
  }

  // 返回扫一扫
  reScan = () => {
    storage.load({ key: cache.USER_INFO }).then(data => {
      this.setState({ isAlliance: data && data.isAlliance }, () => {
        this.querryValidCodeAuth();
      });
    }).catch(err => {
      console.log(err);
    });
  }

  querryValidCodeAuth = () => {
    const { isAlliance } = this.state;
    api.home.querryValidCodeAuth().then(response => {
      if (response.success) {
        const resData = response.data;
        const isManagerIndex = resData.indexOf('oilStationVerification');
        if (isManagerIndex != -1) {
          msg.emit('router: goToNext', {
            routeName: 'QRCodeScanCAV',
            routerFrom: 'WriteOffResult',
            isAlliance,
            oilcardAuthority: true
          });
        } else {
          msg.emit('router: goToNext', {
            routeName: 'QRCodeScanCAV',
            routerFrom: 'WriteOffResult',
            isAlliance,
            oilcardAuthority: false
          });
        }
      } else {
        msg.emit('app:tip', { text: response.msg || '核销失败' });
      }
    });
  }

  // 返回首页
  goHome = () => {
    msg.emit('router: goToNext', {
      routeName: 'Main'
    });
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingVertical: px2dp(100)
  },
  resultImage: {
    width: px2dp(50),
    height: px2dp(50),
  },
  btn: {
    width: px2dp(520),
    height: px2dp(80),
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: fontColorCoffee,
    borderRadius: px2dp(12)
  }
});
